
const app = getApp()
Page({
  data: {
    img:[],
    index:0,
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.getBanner(res.data.agentId)
    })
  },
  onShow: function () {
    app.getMemInfo().then(res=>{
      this.getBanner(res.data.agentId)
    })
  },
  addText(){
    const id = this.data.img[this.data.index].id
    const url = this.data.img[this.data.index].imageUrl
    const imgId = this.data.img[this.data.index].posterImageId
    wx.navigateTo({
      url:'addText?id='+id+'&url='+url+'&imgId='+imgId
    })
  },
  getBanner(agentId){
    const data = {
      agentId
    }
    app.request('wx_app/poster/list',data,'GET').then(res=>{
      console.log(res)
      const img = []
      res.data.forEach(item=>{
        let [start,end] = ['','']
        if (item.startTime!=null) {
          start = item.startTime.split('T')[0]
        }else{
          start = ''
        }
        if (item.endTime!=null) {
          end = item.endTime.split('T')[0]
        }else{
          end = ''
        }
        img.push({
          start:start,
          end:end,
          time:start+'至'+end,
          id:item.id,
          imageUrl:item.imageUrl,
          phone:item.phone,
          address:item.address,
          content:item.Content,
          posterImageId:item.posterImageId
        })
      })
      this.setData({
        img
      })
    })  
  },
  bannerChange(e){
    console.log(e.detail.current)
    this.setData({
      index:e.detail.current
    })
  },
  generateBanner(e){
    const i = e.currentTarget.dataset.index
    const info = JSON.stringify(this.data.img[i])
    console.log(info)
    wx.navigateTo({
      url:'banner?info='+info
    })
  }
})